package com.quocnv.mediascanner;

import java.io.File;
import java.util.ArrayList;
import android.support.v4.app.FragmentActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ShowFileActivity extends FragmentActivity implements
		OnItemClickListener, IMediaStateChangeListener, OnClickListener
{
	String TAG = "MainActivity";
	ListView lvShowFile;
	FileAdapter adapter;
	static int mShowType = 1;
	String mPath = null;
	ArrayList<File> files = new ArrayList<File>();
	BroadcastReceiver receiver;

	Button btnShowAll;
	Button btnShowTree;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_file);
		// mShowType = getIntent().getIntExtra("show_type", 0);
		// mPath = getIntent().getStringExtra("path");

		receiver = new MediaScannerReceiver();

		lvShowFile = (ListView) findViewById(R.id.lvShowFile);
		btnShowAll = (Button) findViewById(R.id.btnShowAll);
		btnShowTree = (Button) findViewById(R.id.btnShowTree);

		btnShowAll.setOnClickListener(this);
		btnShowTree.setOnClickListener(this);
		// ArrayList<File> file1s = FileScanner.getInstance().getAll(
		// "/mnt/usb/sda1", FileFilterUtil.getMediaFilter());
		// ArrayList<File> file2s = FileScanner.getInstance().getFiles(
		// "/mnt/usb/sda1", FileFilterUtil.getMediaFilter());

		// if (mShowType == 0)
		// {
		// files = FileScanner.getInstance().getFiles(mPath,
		// FileFilterUtil.getMediaFilter());
		// }
		// else
		// {
		// files = FileScanner.getInstance().getAll(mPath,
		// FileFilterUtil.getMediaFilter());
		// }

		adapter = new FileAdapter(this, files);
		lvShowFile.setAdapter(adapter);
		lvShowFile.setOnItemClickListener(this);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		IntentFilter intentFilter = new IntentFilter(
				"android.intent.action.MEDIA_MOUNTED");
		intentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
		intentFilter.addDataScheme("file");
		((MediaScannerReceiver) receiver).setListener(this);
		registerReceiver(receiver, intentFilter);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		unregisterReceiver(receiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		File file = (File) parent.getItemAtPosition(position);
		if (file.isDirectory())
		{
			adapter.setFiles(FileScanner.getInstance().getFiles(
					file.getAbsolutePath(), FileFilterUtil.getMediaFilter()));
			adapter.notifyDataSetChanged();
			lvShowFile.setSelection(0);
		}
	}

	@Override
	public void onStateChange(String tag, Object data)
	{
		String path = (String) data;
		if (Intent.ACTION_MEDIA_MOUNTED.equals(tag))
		{
			Log.e(TAG, "ACTION_MEDIA_MOUNTED");
			Log.e(TAG, path);

			mPath = path;
			updateListview();
		}
		else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(tag))
		{
			Log.e(TAG, "ACTION_MEDIA_UNMOUNTED");
			Log.e(TAG, path);

			mPath = null;
			updateListview();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btnShowAll:
			mShowType = 0;
			updateListview();
			break;

		case R.id.btnShowTree:
			mShowType = 1;
			updateListview();
			break;

		default:
			break;
		}
	}

	private void updateListview()
	{
		// mPath = "mnt/usb/sda1";
		if (mPath == null)
		{
			adapter.clear();
			adapter.notifyDataSetChanged();
			return;
		}

		new FileScanTask().execute();
		// ArrayList<File> files = new ArrayList<File>();
		// if (mShowType == 0)
		// files = FileScanner.getInstance().getAll(mPath,
		// FileFilterUtil.getMediaFilter());
		// else
		// {
		// files = FileScanner.getInstance().getFiles(mPath,
		// FileFilterUtil.getMediaFilter());
		// }
		// adapter.setFiles(files);
		// adapter.notifyDataSetChanged();
		// lvShowFile.setSelection(0);
	}

	private class FileScanTask extends AsyncTask<String, Void, ArrayList<File>>
	{

		private ProgressDialog dialog = new ProgressDialog(
				ShowFileActivity.this);

		@Override
		protected void onPreExecute()
		{
			dialog.setCancelable(true);
			dialog.setMessage("Scanning...");
			dialog.setIndeterminate(true);
			dialog.show();
			super.onPreExecute();
		}

		@Override
		protected ArrayList<File> doInBackground(String... params)
		{
			ArrayList<File> files = new ArrayList<File>();
			if (mShowType == 0)
				files = FileScanner.getInstance().getAll(mPath,
						FileFilterUtil.getMediaFilter());
			else
			{
				files = FileScanner.getInstance().getFiles(mPath,
						FileFilterUtil.getMediaFilter());
			}
			return files;
		}

		@Override
		protected void onPostExecute(ArrayList<File> result)
		{
			super.onPostExecute(result);
			if (this.dialog.isShowing())
				this.dialog.dismiss();

			adapter.setFiles(result);
			adapter.notifyDataSetChanged();
			lvShowFile.setSelection(0);
		}

	}
}
